// pages/storeIntroduce/storeIntroduce.js
const app = getApp()
import { requestApi } from '../../api'
import { formatImage } from '../../utils/formatImage'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar_image: '',
    following: '',
    loading: false,
    authorized_brand: [], //授权列表
    seller_rate: [],  //综合评分数据
    excellent_store_image: '',
    storeIntroduce: '',
    followers_count: 0,
    deal_count: 0,
    seller_name: '',
    nickname: '',
    storeIcon: '',
    seller_new_uid: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let channel = options.channel || 'wx_xcx'
    let seller_uid = options.seller_uid
    
    this.data.channel = channel
    this.data.seller_new_uid = options.seller_uid
    this.getStoreDetailData(seller_uid, channel)
  },
  
  getStoreDetailData: function (seller_uid, channel) {
    app.request({
      url: requestApi.storeDetailUrl,
      method: 'GET',
      data: {
        seller_uid: seller_uid,
        channel: channel
      }
    }, (data) => {
      let seller_type, rate, desc, is_excellent
      seller_type = data.original.data.seller_type

      switch (seller_type) {
        case 0:
          this.setData({
            seller_name: '普通用户'
          })
          break;
        case 1:
          this.setData({
            seller_name: '注册买手'
          })
          break; 
        case 2:
          this.setData({
            seller_name: '买手店'
          })
          break; 
        case 3:
          this.setData({
            seller_name: '精品店'
          })
          break; 
        case 4:
          this.setData({
            seller_name: '精品店'
          })
          break;
        case 5:
          this.setData({
            seller_name: '品牌店'
          })
          break;
      }
      if (data.original.data.nickname) {
        this.setData({
          nickname: data.original.data.nickname
        })
      }
      if (data.original.data.avatar_image_url && data.original.data.avatar_image_url.length) {
        data.original.data.avatar_image_url = formatImage.cdnImage(data.original.data.avatar_image_url, formatImage.THUMBNAIL_L)
        
        this.setData({
          avatar_image: data.original.data.avatar_image_url
        })
      }
      this.setData({
        following: data.original.data.following
      }) 
      //授权列表
      if (data.original.data.authorized_brand && data.original.data.authorized_brand.length) {
        this.setData({
          authorized_brand: data.original.data.authorized_brand
        })
      }
      //评分列表
      if (data.original.data.seller_rate && data.original.data.seller_rate.length) {
        this.setData({
          seller_rate: data.original.data.seller_rate
        })
      }
      //店铺介绍
      if (data.original.data.desc.body && data.original.data.desc.body.length) {
        this.setData({
          storeIntroduce: data.original.data.desc.body
        })
      }
      this.setData({
        followers_count: data.original.data.followers_count
      })
      this.setData({
        deal_count: data.original.data.deal_count
      })
      if (data.original.data.excellent_store_image_uri && data.original.data.excellent_store_image_uri.length) {
        data.original.data.excellent_store_image_uri = formatImage.cdnImage(data.original.data.excellent_store_image_uri, formatImage.THUMBNAIL_XS)
      }
      
      this.setData({
        excellent_store_image: data.original.data.excellent_store_image_uri,
        loading: true
      })
    }, error => {
      conssole.log(error)
    })
  },

  changeState: function (e) {
    let pages = getCurrentPages()
    let following = e.currentTarget.id
    let prevPage = pages[pages.length - 2]

    if (following == 1) {
      following = 0
    } else {
        following = 1
    }
    app.request({
      url: requestApi.followuserUrl,
      method: 'POST',
      data: {
        seller_uid: this.data.seller_new_uid,
        status: '' + following
      }
    }, (data) => {
      this.setData({
        following: following
      })

      prevPage.setData({
        following: following
      })
    }, error => {
      console.log(error)
    })
  },

  authTap: function (e) {
    let id = e.currentTarget.id

    wx.navigateTo({
      url: '/pages/categoryHall/categoryHall?keywords=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
})